package com.hdd.biz.painting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hdd.biz.painting.entity.UserFollow;
import com.hdd.biz.painting.mapper.UserFollowMapper;
import com.hdd.biz.painting.service.UserFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户关注表 服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-03-18
 */
@Service
public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFollow> implements UserFollowService {


    @Override
    public void insert(String userId, String follwUserId) {
        UserFollow userFollow = new UserFollow();
        userFollow.setUserId(userId);
        userFollow.setFollowUserId(follwUserId);
        baseMapper.insert(userFollow);
    }

    @Override
    public boolean insertUserFollow(String userId, String follwUserId) {
        UserFollow userFollow = new UserFollow();
        if(!isFollow(userId,follwUserId)){
            userFollow.setUserId(userId);
            userFollow.setFollowUserId(follwUserId);
            return  baseMapper.insert(userFollow)>0?true:false;
        };
        return false;
    }


    @Override
    public boolean isFollow(String userId, String follwUserId) {
        QueryWrapper<UserFollow> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("follow_user_id",follwUserId);

        return baseMapper.selectCount(wrapper)>0?true:false;
    }

    @Override
    public Integer find(String userId, String follwUserId) {
        QueryWrapper<UserFollow> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("follow_user_id",follwUserId);
       return  baseMapper.selectCount(wrapper);
    }

    @Override
    public boolean cancelFollow(String userId, String follwUserId) {
        QueryWrapper<UserFollow> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("follow_user_id",follwUserId);
        return baseMapper.delete(wrapper)>0?true:false;
    }


    @Override
    public boolean mutualFollow(String userId, String follwUserId) {
        QueryWrapper<UserFollow> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("follow_user_id",follwUserId);
        return baseMapper.selectCount(wrapper)>0?true:false;
    }


    @Override
    public int fansCount(String userId, String follwUserId) {
        QueryWrapper<UserFollow> wrapper = new QueryWrapper<>();
        wrapper.eq("follow_user_id",follwUserId);
        wrapper.ne("user_id",userId);

        return baseMapper.selectCount(wrapper);
    }


    @Override
    public int followsCount(String userId, String follwUserId) {
        QueryWrapper<UserFollow> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(userId)){
            wrapper.eq("user_id",userId);
            wrapper.ne("follow_user_id",userId);

        }

        return baseMapper.selectCount(wrapper);
    }

    @Override
    public List<UserFollow> userIds(String follwUserId) {
        QueryWrapper<UserFollow> wrapper = new QueryWrapper<>();
        wrapper.eq("follow_user_id",follwUserId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public int findMyFollowCount(String userId) {
        QueryWrapper<UserFollow> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        return baseMapper.selectCount(wrapper);
    }
}
